#include-once
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#NoTrayIcon


$dll = DllOpen("FFACE.dll")
$running = DllCall($dll, "int", "InitFFACE") ;Initialize the DLL.
$rodpos = DllCall($dll, "int", "GetRodPosition") ;Function to call
$fishhp = DllCall($dll, "int", "GetFishHP") ;Function to call
$event = 0
$GetWin = _WinGetHandleByExe('pol.exe')
$ffxiwinname = WinGetTitle(HWnd($GetWin))
AutoItSetOption("SendKeyDelay", 50)
AutoItSetOption("SendKeyDownDelay", 100)
WinActivate($ffxiwinname)

While 1
	$t = TimerInit()
	Sleep(1000)
	Send("!1")
	Sleep(2000)

	Do
		Sleep(1)
		$fishhp = DllCall($dll, "int", "GetFishHP")
		$rodpos = DllCall($dll, "int", "GetRodPosition")
	Until $fishhp[0] <> 0 Or TimerDiff($t) > 40000
	
	If $fishhp[0] <> 0 Then
		reelin()
	EndIf

	Sleep(4000)
WEnd

Func reelin()
	AutoItSetOption("SendKeyDelay", 6)
	AutoItSetOption("SendKeyDownDelay", 3)
	$fishid = DllCall($dll, "int", "GetFishID1") ;Function to call
	Do
		$rodpos = DllCall($dll, "int", "GetRodPosition")
		$fishhp = DllCall($dll, "int", "GetFishHP")
		If $rodpos[0] = 258 Then
			right()
		EndIf
		If $rodpos[0] = 3 Then
			left()
		EndIf
		If $rodpos[0] = 1 Then
			Send("{NUMPAD6 up}")
			Send("{NUMPAD4 up}")
		EndIf
	Until $fishhp[0] = 0
	Send("{NUMPAD6 up}")
	Send("{NUMPAD4 up}")
	Sleep(500)
	Send("{enter}")
	Sleep(50)
	Send("{enter}")
	Send("{enter}")
	Send("{enter}")
	Sleep(10000)
	AutoItSetOption("SendKeyDelay", 50)
	AutoItSetOption("SendKeyDownDelay", 100)
EndFunc   ;==>reelin


Func left()
	AutoItSetOption("SendKeyDelay", 1)
	AutoItSetOption("SendKeyDownDelay", 100)
	Send("{NUMPAD6 up}")
	Send("{NUMPAD4 down}")
	Do
		Sleep(1)
		$rodpos = DllCall($dll, "int", "GetRodPosition")
		$fishhp = DllCall($dll, "int", "GetFishHP")
	Until $rodpos[0] <> 3 Or $fishhp[0] = 0
	Send("{NUMPAD4 up}")
EndFunc   ;==>left

Func right()
	AutoItSetOption("SendKeyDelay", 1)
	AutoItSetOption("SendKeyDownDelay", 100)
	Send("{NUMPAD4 up}")
	Send("{NUMPAD6 down}")
	Do
		Sleep(1)
		$rodpos = DllCall($dll, "int", "GetRodPosition")
		$fishhp = DllCall($dll, "int", "GetFishHP")
	Until $rodpos[0] <> 258 Or $fishhp[0] = 0
	Send("{NUMPAD6 up}")
EndFunc   ;==>right

Func _WinGetHandleByExe($sExe, $sTitle = '', $sText = '')
	Local $aPlist = ProcessList($sExe)
	Local $aWList = WinList(), $sHold
	For $iCC = 1 To UBound($aWList, 1) - 1
		For $xCC = 1 To UBound($aPlist, 1) - 1
			If WinGetProcess($aWList[$iCC][1]) = $aPlist[$xCC][1] Then
				If $sTitle Then
					If StringInStr($aWList[$iCC][0], $sTitle) Then
						Return $aWList[$iCC][1]
					EndIf
				ElseIf $sText Then
					If StringInStr(WinGetText($aWList[$iCC][0]), $sText) Then
						Return $aWList[$iCC][1]
					EndIf
				Else
					If BitAND(WinGetState($aWList[$iCC][1]), 2) Then Return $aWList[$iCC][1]
				EndIf
			EndIf
		Next
	Next
	Return SetError(1, 0, '')
EndFunc   ;==>_WinGetHandleByExe

DllClose($dll)